package com.fancon.android.gardenjokesx.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.fancon.android.emoji.widget.RichEditText;
import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.FacebookUser;
import com.fancon.android.gardenjokesx.entity.FlickrUser;
import com.fancon.android.gardenjokesx.entity.TumblrUser;
import com.fancon.android.gardenjokesx.entity.TwitterUser;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.entity.WeiboUser;
import com.fancon.android.gardenjokesx.social.DatabaseHelper;
import com.fancon.android.gardenjokesx.social.SocialUntil;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.StringUtil;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.ui.widget.FanconProgressBar;

public class M04_7Activity extends GardenJokesBaseActivity {

	private RichEditText mEditComment;
	private ImageView mImg;
	private Button mBtnPost;
	private Button mBtnTwitter;
	private Button mBtnFacebook;
	private Button mBtnTumblr;
	private Button mBtnFlickr;
	private Button mBtnWeibo;
	private boolean FACEBOOK, TWITTER, TUMBLR, FLICKR, WEIBO;
	private DatabaseHelper mDb;
	private User user;
	private Bundle bundle;
	private String imageUrl;
	private Bitmap bm;
	public static final int REQUEST_FACEBOOK = 125;
	public static final int REQUEST_TWITTER = 126;
	public static final int REQUEST_TUMBLR = 127;
	public static final int REQUEST_FLICKR = 128;
	public static final int REQUEST_WEIBO = 129;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.m04_7);
		try {
			user = UserUtil.getInfoUserLogin(getBaseContext());
			mDb = new DatabaseHelper(getBaseContext());

			if (getIntent().getExtras() != null) {
				bundle = getIntent().getExtras();
				bundle.putString(Constant.X_USERNAME,
						user.getUser_name());
			}
			Log.d(getClass().getName() + "image url",
					bundle.getString(Constant.URL_PHOTO));
			imageUrl = StringUtil.getPhysicImagePath(bundle.getString(Constant.URL_PHOTO),
					this);
			bm = BitmapFactory.decodeFile(imageUrl);
			mImg = (ImageView) findViewById(R.id.m04_7_imgvThumbnail);
			mImg.setImageBitmap(bm);

			mEditComment = (RichEditText) findViewById(R.id.m04_7_editComment);

			mBtnPost = (Button) findViewById(R.id.m04_7_btnPost);
			mBtnPost.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					RequestPost request = new RequestPost("M04-7",
							M04_7Activity.this);
					getGlobalState().getRequestQueue().addRequest(request);

				}
			});
			mBtnFacebook = (Button) findViewById(R.id.m04_7_btnLinkFacebook);
			setButtonDefaultState(mBtnFacebook, user.getUser_id());
			mBtnFacebook.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setOnClick(v);
				}
			});

			mBtnFlickr = (Button) findViewById(R.id.m04_7_btnLinkFlickr);
			setButtonDefaultState(mBtnFlickr, user.getUser_id());
			mBtnFlickr.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setOnClick(mBtnFlickr);
				}
			});
			mBtnTwitter = (Button) findViewById(R.id.m04_7_btnLinkTwitter);
			setButtonDefaultState(mBtnTwitter, user.getUser_id());
			mBtnTwitter.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setOnClick(v);
				}
			});

			mBtnTumblr = (Button) findViewById(R.id.m04_7_btnLinkTumblr);
			setButtonDefaultState(mBtnTumblr, user.getUser_id());
			mBtnTumblr.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setOnClick(mBtnTumblr);
				}
			});
			mBtnWeibo = (Button) findViewById(R.id.m04_7_btnLinkWeibo);
			setButtonDefaultState(mBtnWeibo, user.getUser_id());
			mBtnWeibo.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					setOnClick(mBtnWeibo);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Bundle b = new Bundle();
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK && requestCode == REQUEST_TWITTER) {
			b = data.getExtras();
			TWITTER = b.getBoolean(Constant.LINK_STATE);
		}
		if (resultCode == RESULT_OK && requestCode == REQUEST_FACEBOOK) {
			b = data.getExtras();
			FACEBOOK = b.getBoolean(Constant.LINK_STATE);
		}
		if (resultCode == RESULT_OK && requestCode == REQUEST_FLICKR) {
			b = data.getExtras();
			FLICKR = b.getBoolean(Constant.LINK_STATE);
		}
		if (resultCode == RESULT_OK && requestCode == REQUEST_TUMBLR) {
			b = data.getExtras();
			TUMBLR = b.getBoolean(Constant.LINK_STATE);
		}
		if (resultCode == RESULT_OK && requestCode == REQUEST_WEIBO) {
			b = data.getExtras();
			WEIBO = b.getBoolean(Constant.LINK_STATE);
		}
		setSocialBtnState();
	}

	/**
	 * Set social button state with current state.
	 */
	private void setSocialBtnState() {
		if (FACEBOOK) {
			mBtnFacebook
					.setBackgroundResource(R.drawable.common_icon_facebook_active);
		} else {
			mBtnFacebook
					.setBackgroundResource(R.drawable.common_icon_facebook_deactive);
		}
		if (TWITTER) {
			mBtnTwitter
					.setBackgroundResource(R.drawable.common_icon_twitter_active);
		} else {
			mBtnTwitter
					.setBackgroundResource(R.drawable.common_icon_twitter_deactive);
		}
		if (FLICKR) {
			mBtnFlickr
					.setBackgroundResource(R.drawable.common_icon_flickr_active);
		} else {
			mBtnFlickr
					.setBackgroundResource(R.drawable.common_icon_flickr_deactive);
		}
		if (TUMBLR) {
			mBtnTumblr
					.setBackgroundResource(R.drawable.common_icon_tumblr_active);
		} else {
			mBtnTumblr
					.setBackgroundResource(R.drawable.common_icon_tumblr_deactive);
		}
		if (WEIBO) {
			mBtnWeibo
					.setBackgroundResource(R.drawable.common_icon_weibo_active);
		} else {
			mBtnWeibo
					.setBackgroundResource(R.drawable.common_icon_weibo_deactive);
		}
		return;
	}

	/**
	 * Check button state
	 * 
	 * @param c
	 *            : Context
	 * @param btn
	 *            : Button
	 * @return state of button: ON(true), OFF( false)
	 */
	private boolean getBtnState(Button v) {
		boolean state = Constant.ON;

		switch (v.getId()) {
		case R.id.m04_7_btnLinkFacebook:
			state = FACEBOOK;
			break;
		case R.id.m04_7_btnLinkFlickr:
			state = FLICKR;
			break;
		case R.id.m04_7_btnLinkTwitter:
			state = TWITTER;
			break;
		case R.id.m04_7_btnLinkTumblr:
			state = TUMBLR;
			break;
		case R.id.m04_7_btnLinkWeibo:
			state = WEIBO;
			break;
		default:
			state = Constant.OFF;
			break;
		}
		return state;
	}

	/**
	 * 
	 * @param btn
	 * @param userId
	 */
	private void setBtnState(Button v, String userId) {

		boolean currentState = getBtnState(v);

		if (currentState == Constant.ON) {// if button is active
			switch (v.getId()) {
			case R.id.m04_7_btnLinkFacebook:
				v.setBackgroundResource(R.drawable.common_icon_facebook_deactive);
				FACEBOOK = Constant.OFF;
				break;
			case R.id.m04_7_btnLinkFlickr:
				v.setBackgroundResource(R.drawable.common_icon_flickr_deactive);
				FLICKR = Constant.OFF;
				break;
			case R.id.m04_7_btnLinkTwitter:
				v.setBackgroundResource(R.drawable.common_icon_twitter_deactive);
				TWITTER = Constant.OFF;
				break;
			case R.id.m04_7_btnLinkTumblr:
				v.setBackgroundResource(R.drawable.common_icon_tumblr_deactive);
				TUMBLR = Constant.OFF;
				break;
			case R.id.m04_7_btnLinkWeibo:
				v.setBackgroundResource(R.drawable.common_icon_weibo_deactive);
				WEIBO = Constant.OFF;
				break;
			default:
				break;
			}
		} else if (currentState == Constant.OFF) {

			Bundle b = new Bundle();
			b.putInt(Constant.SCREEN_ID, Constant.M04_7);
			b.putBoolean(Constant.LINK_STATE, Constant.OFF);

			switch (v.getId()) {
			case R.id.m04_7_btnLinkFacebook:
				if (mDb.isFacebookLinked(userId)) {
					v.setBackgroundResource(R.drawable.common_icon_facebook_active);
					FACEBOOK = Constant.ON;
				} else {
					Intent i = new Intent(getBaseContext(), S03_2Activity.class);
					i.putExtras(b);
					startActivityForResult(i, REQUEST_FACEBOOK);
				}
				break;
			case R.id.m04_7_btnLinkFlickr:
				if (mDb.isFlickrLinked(userId)) {
					v.setBackgroundResource(R.drawable.common_icon_flickr_active);
					FLICKR = Constant.ON;
				} else {
					Intent i = new Intent(getBaseContext(), S03_3Activity.class);
					i.putExtras(b);
					startActivityForResult(i, REQUEST_FLICKR);
				}
				break;
			case R.id.m04_7_btnLinkTwitter:
				if (mDb.isTwitterLinked(userId)) {
					v.setBackgroundResource(R.drawable.common_icon_twitter_active);
					TWITTER = Constant.ON;
				} else {
					Intent i = new Intent(getBaseContext(), S03_1Activity.class);
					i.putExtras(b);
					startActivityForResult(i, REQUEST_TWITTER);
				}
				break;
			case R.id.m04_7_btnLinkTumblr:
				if (mDb.isTumblrLinked(userId)) {
					v.setBackgroundResource(R.drawable.common_icon_tumblr_active);
					TUMBLR = Constant.ON;
				} else {
					Intent i = new Intent(getBaseContext(), S03_4Activity.class);
					i.putExtras(b);
					startActivityForResult(i, REQUEST_TUMBLR);
				}
				break;
			case R.id.m04_7_btnLinkWeibo:
				if (mDb.isWeiboLinked(userId)) {
					v.setBackgroundResource(R.drawable.common_icon_weibo_active);
					WEIBO = Constant.ON;
				} else {
					Intent i = new Intent(getBaseContext(), S03_7Activity.class);
					i.putExtras(b);
					startActivityForResult(i, REQUEST_WEIBO);
				}
				break;
			default:
				break;
			}
		}
	}

	/**
	 * Set button with appropriate background and text.
	 * 
	 * @param btn
	 *            : Button.
	 * @param state
	 *            : Boolean( ON-true, OFF-false). State to set to button.
	 */
	private void setButtonDefaultState(Button btn, String userId) {
		switch (btn.getId()) {
		case R.id.m04_7_btnLinkFacebook:
			if (mDb.isFacebookLinked(userId)) {
				FACEBOOK = Constant.ON;
				btn.setBackgroundResource(R.drawable.common_icon_facebook_active);
			} else {
				FACEBOOK = Constant.OFF;
				btn.setBackgroundResource(R.drawable.common_icon_facebook_deactive);
			}
			break;
		case R.id.m04_7_btnLinkTumblr:
			if (mDb.isTumblrLinked(userId)) {
				TUMBLR = Constant.ON;
				btn.setBackgroundResource(R.drawable.common_icon_tumblr_active);
			} else {
				TUMBLR = Constant.OFF;
				btn.setBackgroundResource(R.drawable.common_icon_tumblr_deactive);
			}
			break;
		case R.id.m04_7_btnLinkTwitter:
			if (mDb.isTwitterLinked(userId)) {
				TWITTER = Constant.ON;
				btn.setBackgroundResource(R.drawable.common_icon_twitter_active);
			} else {
				TWITTER = Constant.OFF;
				btn.setBackgroundResource(R.drawable.common_icon_twitter_deactive);
			}
			break;
		case R.id.m04_7_btnLinkFlickr:
			if (mDb.isFlickrLinked(userId)) {
				FLICKR = Constant.ON;
				btn.setBackgroundResource(R.drawable.common_icon_flickr_active);
			} else {
				FLICKR = Constant.OFF;
				btn.setBackgroundResource(R.drawable.common_icon_flickr_deactive);
			}
			break;
		case R.id.m04_7_btnLinkWeibo:
			if (mDb.isWeiboLinked(userId)) {
				WEIBO = Constant.ON;
				btn.setBackgroundResource(R.drawable.common_icon_weibo_active);
			} else {
				WEIBO = Constant.OFF;
				btn.setBackgroundResource(R.drawable.common_icon_weibo_deactive);
			}
			break;
		default:
			break;
		}
	}

	private void setOnClick(View v) {
		// Log.v("click", "click state: "+getBtnState((Button)v));
		setBtnState((Button) v, user.getUser_id());
	}

	/**
	 * Post image and comment to social network
	 */
	private void postToSocialNetwork() {
		Log.d("M04_7", "share photo");
		SocialUntil social = new SocialUntil(this);
		boolean result = false;

		if (FACEBOOK) {
			Log.d("M04_7", "post to facebook");
			FacebookUser fbUser = mDb.getFacebookUser(user.getUser_id());
			if (fbUser != null) {
				String accessToken = fbUser.getAccessToken();
				String facebookUsername = fbUser.getUsername();
				Log.d("M04_7", "facebook user: " + facebookUsername);
				Log.d("M04_7", "facebook message: "
						+ getFacebookMessage(bundle));
				bundle.putString(Constant.FACEBOOK_1ST_NAME, facebookUsername);
				Log.d("M04_7", "facebook message: "
						+ getFacebookMessage(bundle));
				result = social.postToFacebook(imageUrl,
						getFacebookMessage(bundle), accessToken);
				Log.d("M04_7", "Post to facebook: " + result);
			}
		}

		if (TUMBLR) {
			TumblrUser tumUser = mDb.getTumblrUser(user.getUser_id());
			String email = tumUser.getUsername();
			String password = tumUser.getPassword();
			result = social.postToTumblr(imageUrl, getTumblrMessage(bundle),
					email, password);
			Log.v("M04_7", "Post to tumblr: ");
		}

		if (TWITTER) {
			TwitterUser twiUser = mDb.getTwitterUser(user.getUser_id());
			String token = twiUser.getTwitterToken();
			String secret = twiUser.getTwitterSecret();
			result = social.postToTwitter(imageUrl, getTwitterMessage(bundle),
					token, secret);
			Log.v("M04_7", "post Twitterresult: " + result);
		}

		if (FLICKR) {
			FlickrUser flUser = mDb.getFlickrUser(user.getUser_id());
			String token = flUser.getAccessToken();
			result = social.postToFlickr(imageUrl, getFlickrMessage(bundle),
					"", token);
			Log.v("M04_7", "post to flickr result: " + result);
		}

		if (WEIBO) {
			WeiboUser weiUser = mDb.getWeiboUser(user.getUser_id());
			String token = weiUser.getWeiboToken();
			String secret = weiUser.getWeiboSecret();
			result = social.postToWeibo(imageUrl, getWeiboMessage(bundle), "",
					"", token, secret);
			Log.v("post Weibo", "result: " + result);
		}

		bm = null;
		return;
	}

	/**
	 * Create message to post to facebook.
	 * 
	 * @param b
	 */
	private String getFacebookMessage(Bundle b) {
		String msg = null;
		String message = mEditComment.getRichText().toString();
		String place = "";
		String friend = "";

		if (b.getString(Constant.PLACE) != null
				&& b.getString(Constant.PLACE).length() > 0) {
			place = " " + b.getString(Constant.PLACE);
		}
		if (b.getString(Constant.TAGGED_FRIEND) != null
				&& b.getString(Constant.TAGGED_FRIEND).length() > 0) {
			friend = " with " + b.getString(Constant.TAGGED_FRIEND);
		}

		switch (b.getInt(Constant.TAGGED_MUMBERS)) {
		case 2:
			// Check input message.
			if (message.length() > 0) {

				msg = getResources().getString(
						R.string.M04_7_FACEBOOK_2TAGS_MESSAGE, message,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_FACEBOOK_2TAGS,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;

		case 1:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_FACEBOOK_1TAG_MESSAGE, message,
						b.getString(Constant.TAG_NAME1), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_FACEBOOK_1TAG,
						b.getString(Constant.TAG_NAME1), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;
		default:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_FACEBOOK_0TAG_MESSAGE, message, place,
						friend, b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_FACEBOOK_0TAG,
						place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;
		}
		Log.d("M04_7", "getFacebookMessage:  " + msg);
		return msg;
	}

	/**
	 * Create message to post to tumblr.
	 * 
	 * @param b
	 */
	private String getTumblrMessage(Bundle b) {
		String msg = null;
		String message = mEditComment.getRichText().toString();
		String place = "";
		String friend = "";

		if (b.getString(Constant.PLACE) != null
				&& b.getString(Constant.PLACE).length() > 0) {
			place = " " + b.getString(Constant.PLACE);
		}
		if (b.getString(Constant.TAGGED_FRIEND) != null
				&& b.getString(Constant.TAGGED_FRIEND).length() > 0) {
			friend = " with " + b.getString(Constant.TAGGED_FRIEND);
		}
		switch (b.getInt(Constant.TAGGED_MUMBERS)) {
		case 2:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_TUMBLR_2TAGS_MESSAGE, message,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_TUMBLR_2TAGS,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;

		case 1:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_TUMBLR_1TAG_MESSAGE, message,
						b.getString(Constant.TAG_NAME1), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_TUMBLR_1TAG,
						b.getString(Constant.TAG_NAME1), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;

		default:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_TUMBLR_0TAG_MESSAGE, message, place,
						friend, b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_TUMBLR_0TAG,
						place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;
		}
		Log.d("M04_7", "getTumblrMessage:  " + msg);
		return msg;
	}

	/**
	 * Create message to post to flickr.
	 * 
	 * @param b
	 */
	private String getFlickrMessage(Bundle b) {
		String msg = null;
		String message = mEditComment.getRichText().toString();
		String place = "";
		String friend = "";

		if (b.getString(Constant.PLACE) != null
				&& b.getString(Constant.PLACE).length() > 0) {
			place = "at " + b.getString(Constant.PLACE) + " ";
		}
		if (b.getString(Constant.TAGGED_FRIEND) != null
				&& b.getString(Constant.TAGGED_FRIEND).length() > 0) {
			friend = " with " + b.getString(Constant.TAGGED_FRIEND);
		}

		switch (b.getInt(Constant.TAGGED_MUMBERS)) {
		case 2:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_FLICKR_2TAGS_MESSAGE, message,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_FLICKR_2TAGS,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;
		case 1:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_FLICKR_1TAG_MESSAGE, message,
						b.getString(Constant.TAG_NAME1), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_FLICKR_1TAG,
						b.getString(Constant.TAG_NAME1), place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;
		default:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_FLICKR_0TAG_MESSAGE, message, place,
						friend, b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_FLICKR_0TAG,
						place, friend,
						b.getString(Constant.X_USERNAME),
						b.getString(Constant.URL));
			}
			break;
		}
		Log.d("M04_7", "getFlickrMessage:  " + msg);
		return msg;
	}

	/**
	 * Create message to post to twitter.
	 * 
	 * @param b
	 */
	private String getTwitterMessage(Bundle b) {
		String msg = null;
		String message = mEditComment.getRichText().toString();
		String place = "";

		if (b.getString(Constant.PLACE) != null
				&& b.getString(Constant.PLACE).length() > 0) {
			place = " @ " + b.getString(Constant.PLACE);
		}

		switch (b.getInt(Constant.TAGGED_MUMBERS)) {
		case 2:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_TWITTER_2TAGS_MESSAGE, message,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place,
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_TWITTER_2TAGS,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place,
						b.getString(Constant.URL));
			}
			break;

		case 1:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_TWITTER_1TAG_MESSAGE, message,
						b.getString(Constant.TAG_NAME1), place,
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_TWITTER_1TAG,
						b.getString(Constant.TAG_NAME1), place,
						b.getString(Constant.URL));
			}
			break;
		default:
			// Check input message.
			if (message.length() > 0) {
				if (place.length() > 0) {
					place = " (" + place + ")";
				}
				msg = getResources().getString(
						R.string.M04_7_TWITTER_0TAG_MESSAGE, message, place,
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_TWITTER_0TAG,
						place, b.getString(Constant.URL));
			}
			break;
		}
		Log.d("M04_7", "getTwitterMessage:  " + msg);
		return msg;
	}

	/**
	 * Create message to post to twitter.
	 * 
	 * @param b
	 */
	private String getWeiboMessage(Bundle b) {
		String msg = null;
		String message = mEditComment.getRichText().toString();
		String place = "";

		if (b.getString(Constant.PLACE) != null
				&& b.getString(Constant.PLACE).length() > 0) {
			place = " @ " + b.getString(Constant.PLACE);
		}

		switch (b.getInt(Constant.TAGGED_MUMBERS)) {
		case 2:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_WEIBO_2TAGS_MESSAGE, message,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place,
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_WEIBO_2TAGS,
						b.getString(Constant.TAG_NAME1),
						b.getString(Constant.TAG_NAME2), place,
						b.getString(Constant.URL));
			}
			break;

		case 1:
			// Check input message.
			if (message.length() > 0) {
				msg = getResources().getString(
						R.string.M04_7_WEIBO_1TAG_MESSAGE, message,
						b.getString(Constant.TAG_NAME1), place,
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_WEIBO_1TAG,
						b.getString(Constant.TAG_NAME1), place,
						b.getString(Constant.URL));
			}
			break;

		default:
			// Check input message.
			if (message.length() > 0) {
				if (place.length() > 0) {
					place = " (" + place + ")";
				}
				msg = getResources().getString(
						R.string.M04_7_WEIBO_0TAG_MESSAGE, message, place,
						b.getString(Constant.URL));
			} else {
				msg = getResources().getString(R.string.M04_7_WEIBO_0TAG,
						place, b.getString(Constant.URL));
			}
			break;
		}
		Log.d("M04_7", "getWeiboMessage:  " + msg);
		return msg;
	}

	class RequestPost extends RequestUI {
		private Activity context;
		private FanconProgressBar progress;


		public RequestPost(Object key, Activity activity) {
			super(key, activity);
			context = activity;
			progress = FanconProgressBar.show(context,
					context.getString(R.string.loading));
		}

		@Override
		public void execute() throws Exception {
			postToSocialNetwork();
		}

		@Override
		public void executeUI(Exception ex) {
			progress.dismiss();
			finish();
			// progressbar.setCancelable(true);
			// progressbar.dismiss();
			// move to M04
			// @binhbt-comment
			// Intent intent = new Intent(getBaseContext(), A02Activity.class);
			// intent.putExtra(Constant.SCREEN_ID, Constant.M04_7);
			// startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP));

		}

	}

}
